/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import hyper.market.beans.StockBean;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mikeno
 */
public class searchStockServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Object id = request.getSession().getAttribute("id");
        if (id == null) {
            request.getRequestDispatcher("web_edit/Reject.html").forward(request, response);
        }
        String mySqlJndiName = this.getServletContext().getInitParameter("MySql JNDI"); // get the SQL connection

        String searchValue = request.getParameter("stock");  // get the value of the search
        int storeid = (Integer) request.getSession().getAttribute("storeId");  // get the session store id
        if (searchValue != null) {
            searchValue = searchValue.trim().toLowerCase();
        }
            StockBean stock = new StockBean();
            ArrayList resultList = stock.searchStockByName(searchValue, storeid, mySqlJndiName);
            StringBuilder sb = new StringBuilder();
            for (int count = 0; count < resultList.size(); count++) {
                sb.append("<stockDetails>");
                sb.append("<id>" + ((StockBean) resultList.get(count)).getStockdetailid() + "</id>");
                sb.append("<image>" + ((StockBean) resultList.get(count)).getStockImage() + "</image>");
                sb.append("<stockName>" + ((StockBean) resultList.get(count)).getStockname() + "</stockName>");
                sb.append("<costPrice>" + ((StockBean) resultList.get(count)).getCostprice() + "</costPrice>");
                sb.append("<sellingPrice>" + ((StockBean) resultList.get(count)).getSellingprice() + "</sellingPrice>");
                sb.append("<quantity>" + ((StockBean) resultList.get(count)).getQuantity() + "</quantity>");
                sb.append("<threshold>" + ((StockBean) resultList.get(count)).getThreshold() +"</threshold>");
                sb.append("<status>" + ((StockBean) resultList.get(count)).getStatus() + "</status>");
                sb.append("<discount>" + ((StockBean) resultList.get(count)).getDiscount() + "</discount>");
                sb.append("<expiry>" + ((StockBean) resultList.get(count)).getExpiryDate() + "</expiry>");
                sb.append("</stockDetails>");
            }

            if (resultList.size() != 0) {
                response.setContentType("text/xml");
                response.setHeader("Cache-Control", "no-cache");
                response.getWriter().write("<stocks>"
                        + sb.toString() + "</stocks>");
            } else {
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
